package global

import (
	"fmt"
	"github.com/spf13/viper"
	"gorm.io/gorm"
	"lib/log"
	"lib/structs/base"
	"os"
	"path/filepath"
)

var (
	Conf                   = new(base.VapConf)
	DB                     *gorm.DB
	VaCacheForCpuAndMemory = make(map[int64][]float64, 0)
)

func InitConfig(configName string) {
	execPath, err := os.Executable()
	if err != nil {
		panic(fmt.Sprintf("加载配置文件失败： %v", err))
	}
	v := viper.New()
	v.SetConfigType("yaml")
	v.SetConfigName(configName)
	v.AddConfigPath(filepath.Dir(execPath))
	err = v.ReadInConfig()
	if err != nil {
		panic(fmt.Sprintf("加载配置文件失败： %v", err))
	}
	err = v.Unmarshal(Conf)
	if err != nil {
		panic(fmt.Sprintf("不合法的配置信息： %v", err))
	}
	if Conf.AppFolder == "" {
		Conf.AppFolder = filepath.Dir(execPath)
	}
	log.InitLog(filepath.Join(Conf.AppFolder, "logs", Conf.Log.Name), Conf.Log.Lv)
}
